package view.component.message
{
import com.greensock.TweenLite;

import core.StageProxy;

import flash.display.DisplayObject;
import flash.text.TextField;

import framework.core.ComponentBuilder;
import framework.view.components.Component;

/**
 * Message
 * @author jliang
 *
 */
public class Message extends Component implements IMessage
{
	//==========================================================================
	//  Class constants
	//==========================================================================
	public static function show(data:*, ref:Class = null):IMessage
	{
		var msg:IMessage = ComponentBuilder.create(ref ? ref : Message) as IMessage;
		msg.data = data;
		msg.show();
		return msg;
	}	
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function Message()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var _data:*;
	
	public function set data(value:*):void
	{
		_data = value;
		
		if (data is String)
		{
			label.htmlText = data;
		}
		
		resize();
	}
	
	public function get data():*
	{
		return _data;
	}
	
	protected var bg:DisplayObject;
	
	protected var label:TextField;
	
	protected var marginX:int;
	
	protected var marginY:int;
	//==========================================================================
	//  Overridden methods: Component
	//==========================================================================
	override protected function get skinReference():String
	{
		return "swf.ui.MessageSkin";
	}
	
	override public function initialize():void
	{
		super.initialize();
		
		bg = skin.bg;
		label = skin.label;
		
		marginX = label.x;
		marginY = label.y;
	}
	
	override public function dispose():void
	{
		TweenLite.killTweensOf(this);
		
		super.dispose();
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function show():void
	{
		this.x = StageProxy.screenWidth - this.width - 10;
		this.y = StageProxy.screenHeight - this.height - 90;
		StageProxy.stage.addChild(this);
		
		TweenLite.to(this, 3, { onComplete:close });
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function resize():void
	{
		label.width = label.textWidth + 6;
		label.height = label.textHeight + 6;
		
		bg.width = label.width + marginX * 2;
		bg.height = label.height + marginY * 2;
	}
	
	protected function close():void
	{
		if (parent)
		{
			parent.removeChild(this);
		}		
		dispose();
	}
}
}





